package com.itheima.utils;

import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Objects;
import java.util.concurrent.CopyOnWriteArrayList;

public class CalcUtil {

    private static DecimalFormat formater = new DecimalFormat("#0.00");

    private final static String ADD = "+";
    private final static String MINUS = "-";
    private final static String MULTIP = "*";
    private final static String DIVISION = "/";
    public final static String ZERO = "0";

    /**
     * 保留两位小数
     */
    public static String format(BigDecimal number) {
        ArrayList arrayList = new ArrayList();
        if (Objects.isNull(number)) {
            return null;
        }
        return formater.format(number);
    }

    /**
     * 加法计算
     */
    public static String add(Object o1, Object o2) {
        return calc(ADD, o1, o2);
    }
    public static String add(Object... o) {
        return calc(ADD, o);
    }

    /**
     * 减法计算
     */
    public static String sub(Object o1, Object o2) {
        return calc(MINUS, o1, o2);
    }
    public static String sub(Object... o) {
        return calc(MINUS, o);
    }

    /**
     * 乘法计算
     * @param o1
     * @param o2
     * @return
     */
    public static String mul(Object o1, Object o2) {
        return calc(MULTIP, o1, o2);
    }
    public static String mul(Object... o) {
        return calc(MULTIP, o);
    }

    /**
     * 除法计算
     */
    public static String div(Object o1, Object o2) {
        return calc(DIVISION, o1, o2);
    }
    public static String div(Object... o) {
        return calc(DIVISION, o);
    }


    /**
     * 统一批量计算
     */
    private static String calc(String symbol, Object... o) {
        if(null == o || o.length==0){
            return ZERO;
        }
        String result = String.valueOf(o[0]);
        result = StringUtils.isEmpty(result)?ZERO:result;
        int length = o.length;
        for(int i = 1; i<length; i++){
            result = calc(symbol, result, o[i]);
        }
        return result;
    }
    /**
     * 统一计算
     */
    private static String calc(String symbol, Object o1, Object o2) {
        String s1 = String.valueOf(o1);
        s1 = StringUtils.isEmpty(s1)?ZERO:s1;

        String s2 = String.valueOf(o2);
        s2 = StringUtils.isEmpty(s2)?ZERO:s2;

        BigDecimal b1 = new BigDecimal(s1);
        BigDecimal b2 = new BigDecimal(s2);
        DecimalFormat formater = new DecimalFormat("#0.00"); //保留2位小数 不四舍五入
        formater.setRoundingMode(RoundingMode.HALF_UP);
        String result = ZERO;
        switch (symbol){
            case ADD:
                result = formater.format(b1.add(b2));
                break;
            case MINUS:
                result = formater.format(b1.subtract(b2));
                break;
            case MULTIP:
                result = formater.format(b1.multiply(b2));
                break;
            case DIVISION:
                result = formater.format(b1.divide(b2,2,RoundingMode.HALF_UP));
                break;
            default:
                break;
        }
        return result;
    }

    /*数字比较大小*/
    public static int compareTo(Double v1, Double v2){
        v1 = v1 == null ? 0D : v1;
        v2 = v2 == null ? 0D : v2;

        BigDecimal b1 = new BigDecimal(v1.toString());
        BigDecimal b2 = new BigDecimal(v2.toString());

        return b1.compareTo(b2);
    }
    /*数字比较大小
     * 1：v1>v2; 0:v1=v2; -1:v1<v2
     * */
    public static int compareTo(String v1, String v2){
        v1 = StringUtils.isEmpty(v1) ? "0" : v1;
        v2 = StringUtils.isEmpty(v2) ? "0" : v2;

        v1 = StringUtils.isEmpty(v1) ? "0" : v1;
        v2 = StringUtils.isEmpty(v2) ? "0" : v2;

        BigDecimal b1 = new BigDecimal(v1);
        BigDecimal b2 = new BigDecimal(v2);

        return b1.compareTo(b2);
    }
    public static String compareToBigger(Object o1, Object o2){
        String s1 = String.valueOf(o1);
        s1 = StringUtils.isEmpty(s1)?ZERO:s1;

        String s2 = String.valueOf(o2);
        s2 = StringUtils.isEmpty(s2)?ZERO:s2;

        BigDecimal b1 = new BigDecimal(s1);
        BigDecimal b2 = new BigDecimal(s2);
        if(b1.compareTo(b2)>=0){
            return b1.toString();
        } else {
            return b2.toString();
        }
    }
    public static String compareToSmaller(Object o1, Object o2){
        String s1 = String.valueOf(o1);
        s1 = StringUtils.isEmpty(s1)?ZERO:s1;

        String s2 = String.valueOf(o2);
        s2 = StringUtils.isEmpty(s2)?ZERO:s2;

        BigDecimal b1 = new BigDecimal(s1);
        BigDecimal b2 = new BigDecimal(s2);
        if(b1.compareTo(b2)>=0){
            return b2.toString();
        } else {
            return b1.toString();
        }
    }

    //大于
    public static boolean gt0(Object o) {
        String s1 = String.valueOf(o);
        s1 = StringUtils.isEmpty(s1)?ZERO:s1;
        return new BigDecimal(s1).compareTo(BigDecimal.ZERO)>0;
    }
    //大于等于0
    public static boolean gtEq0(Object o) {
        String s1 = String.valueOf(o);
        s1 = StringUtils.isEmpty(s1)?ZERO:s1;
        if(s1.equals(ZERO)){
            return true;
        }
        return new BigDecimal(s1).compareTo(BigDecimal.ZERO)>=0;
    }
    //等于0
    public static boolean eq0(Object o) {
        String s1 = String.valueOf(o);
        s1 = StringUtils.isEmpty(s1)?ZERO:s1;
        if(s1.equals(ZERO)){
            return true;
        }
        return new BigDecimal(s1).compareTo(BigDecimal.ZERO)==0;
    }
    //大于 o1 是否 大于o2
    public static boolean gt(Object o1, Object o2) {
        String s1 = String.valueOf(o1);
        s1 = StringUtils.isEmpty(s1)?ZERO:s1;
        String s2 = String.valueOf(o2);
        s2 = StringUtils.isEmpty(s2)?ZERO:s2;
        return new BigDecimal(s1).compareTo(new BigDecimal(s2))>0;
    }

    /** 截取小数点前面的整数部分  用于某些使用工具类计算的  数量  包含两位小数的问题 */
    public static String DecimalToInteger(Object o){
        String s1 = String.valueOf(o);
        if (StringUtils.isEmpty(s1)){
            return ZERO;
        }else if (!s1.contains(".")){
            return s1;
        }else{
            return s1.substring(0,s1.indexOf("."));
        }
    }
}
